function readMsgCount() {
    if ($('.noti_count').length > 0) {
        $.ajax({
            headers:{
                'X-CSRF-TOKEN' : csrfToken
            },
            url     : baseUrl+'/admin/ajax/notification',
            data    : {type:'count'},
            dataType: 'json',
            method  : 'POST',
            success: function (resp) {
                if (resp.code == 10000 && resp.data) {
                    $('.noti_count').text(resp.data);
                } else {
                    $('.noti_count').remove();
                }
            }
        });
    }
}

/**
 * 点击提醒列表时间触发
 */
$('#notic').on('shown.bs.dropdown', function () {
    return $.ajax({
        headers:{
            'X-CSRF-TOKEN' : csrfToken
        },
        data    : {type:'list'},
        url     : baseUrl+'/admin/ajax/notification',
        dataType: 'json',
        method  : 'POST',
        success: function (resp) {
            if (resp.code == 10000) {
                if (resp.data.length > 0) {
                    notic_append(resp.data);
                } else {
                    $('.notifications').html('<li class="top" id="notic_top"> ' +
                        '<p class="small text-center">暂无未读消息 您可进入<a href="'+baseUrl+'/admin/notify">消息中心</a></p> ' +
                        '</li>');
                }
            } else {
                $('#notic_top').children('p').html('获取失败...');
            }
        }
    });
});

function notic_append (list) {
    var notic = '<li><li class="top">' +
        '<p class="small"> ' +
        '<a href="'+baseUrl+'/admin/notify" class="pull-right">查看全部</a>' +
        '<a href="javascript:;" class="pull-left" onclick="flag_all()">全部已读</a>'+
        '</p> ' +
        '</li><ul class="dropdown-menu-list list-unstyled ps-scrollbar">';
    for (var o in list) {
        var not = '';
        var type = list[o]['type'];
        var icon = '';
        var style = '';
        switch (type) {
            case 'user'    : icon = 'fa-user';      style = 'info'; break;
            case 'pend'    : icon = 'fa-filter';    style = 'danger'; break;
            case 'like'    : icon = 'fa-thumbs-up'; style = 'primary'; break;
            case 'comment' : icon = 'fa-comment';   style = 'success'; break;
            case 'share'   : icon = 'fa-share-alt'; style = 'warning'; break;
            case 'at'      : icon = 'fa-at';        style = 'secondary'; break;
            default        : icon = 'fa-ellipsis-h';style = 'purple'; break;
        }
        not = '<li class="active notification-'+style+'"> ' +
            '<a href="#"> ' +
            '<i class="'+icon+'"></i> ' +
            '<span class="line"> ' +
            '<strong>'+list[o]['content']+'</strong> ' +
            '</span> ' +
            '<span class="line small time">'+list[o]['created_at']+'</span> ' +
            '</a> ' +
            '</li>';
        notic += not;
    }
    notic += '</ul></li>';
    $('#notic_top').parent().html(notic);
}


/**
 * 标记已读触发  全部
 */
function flag_all() {
    $.ajax({
        headers : {
            'X-CSRF-TOKEN' : csrfToken
        },
        url     : baseUrl+'/admin/ajax/flag',
        dataType: 'json',
        method  : 'POST',
        success : function (resp){
            if (resp.code == '10000') {
                toastr.success(resp.msg);
                $('.notifications').html('<li class="top" id="notic_top"> ' +
                    '<p class="small text-center">暂无未读消息 您可进入<a href="'+baseUrl+'/admin/notify">消息中心</a></p> ' +
                    '</li>');
                $('.timeline-centered').children('article').slideUp(300);
            } else {
                toastr.error(resp.msg);
            }
        }
    });
}

/**
 * 标记一个
 * @param not_id
 */
function flag_one(not_id, object) {
    var obj = $(object);
    $.ajax({
        headers : {
            'X-CSRF-TOKEN' : csrfToken
        },
        url     : baseUrl+'/admin/ajax/flag',
        data    : {notid:not_id},
        dataType: 'json',
        method  : 'POST',
        success : function (resp){
            if (resp.code == '10000') {
                toastr.success(resp.msg);
                obj.parent().parent().parent().parent().slideUp(300);
            } else {
                toastr.error(resp.msg);
            }
        }
    });
}

/**
 * 加载更多通知
 */
$('#moreNotify').on('click', function () {
    var length = $('.timeline-centered').children('article').length;
    var page = (Math.ceil((length-1)/10));
    var status = $(this).attr('data-status');
    $.ajax({
        headers     :   {
            'X-CSRF-TOKEN' : csrfToken
        },
        data    : {page:page,status:status},
        url     : baseUrl+'/admin/ajax/moreNotify',
        dataType: 'json',
        method  : 'POST',
        success: function (resp) {
            if (resp.code == '10000') {
                var data = resp.data;
                if (data.length > 0) {
                    for (var o in data) {
                        var left = '';
                        var remove = '';
                        if (data[o]['not_id']%2 == 0) {
                            left = 'left-aligned';
                        }
                        if (unread) {
                            remove = '<a href="javascript:;" class="pull-right"> ' +
                                '<i class="fa-remove" onclick="flag_one('+data[o]['not_id']+', this)"></i> ' +
                                '</a>';
                        }
                        $('.begin').before('<article class="timeline-entry '+left+'"> ' +
                            '<div class="timeline-entry-inner"> ' +
                            '<time class="timeline-time"><span>'+data[o]['created_at']+'</span></time> ' +
                            '<div class="timeline-icon timeline-bg-'+data[o]['s']+'"> ' +
                            '<i class="'+data[o]['i']+'"></i></div> ' +
                            '<div class="timeline-label"> ' +
                            '<h2><span>'+data[o]['t']+'</span> '+remove+'</h2><p></p></div></div></article>');
                    }
                } else {
                    toastr.info('已经到底了');
                }
            } else {
                toastr.error(resp.msg);
            }
        }
    });
});